<?php
class  Manage_books extends CI_Model
{
        function __construct()
        {
                parent::__construct();
        }
        function category_name($cate_id)
        {
                $sql="select `type` from `classify` where `classify_id`='$cate_id'";
                $query=$this->db->query($sql);
                if($query->num_rows()>0)
                {
                         return $query->row_array();
                }
                else
                {
                        return false;//no category records
                }
        }
        function get_category()
        {//Get the category
                $sql="select `classify_id`,`type` from `classify` where `tree`=1 order by classify_id asc";
                $query=$this->db->query($sql);
                if($query->num_rows()>0)
                {
                        return $query->result_array();
                }
                else
                {
                        return false;//no category records
                }
        }
        function get_second_category($fid)
        {
                $sql="select `classify_id`,`type` from `classify` where `fid`=$fid";
                $query=$this->db->query($sql);
                if($query->num_rows()>0)
                {
                        return $query->result_array();
                }
                else
                {
                        return false;
                }
        }
        function get_book_message($b_id)
        {
                $sql="select * from `books` where `b_id`='$b_id'";
                $query=$this->db->query($sql);
                if($query->num_rows()>0)
                {
                $msg=$query->row_array();
                $message=array('sid'=>$msg['sid'],'b_id'=>$msg['b_id'],'pid'=>$msg['prod_id'],'isbn'=>$msg['isbn'],'bn'=>$msg['name'],'au'=>$msg['author'],'pn'=>$msg['publisher'],'pc'=>$msg['price'],'cp'=>base_url().$msg['dir_path'],'bd'=>$msg['describe'],'bip'=>base_url().$msg['pic'],'pd'=>$msg['pub_date'],'star'=>$msg['star'],'bp'=>base_url().$msg['b_path']);
                return $message;
                }
        }

        function get_book_slave($b_id)
        {
                $sql="select `type_id` from `slave` where `b_id`='$b_id' order by `slave_id`";
                $query=$this->db->query($sql);
                return $query->result_array();
        }

        function get_book_series_id($b_id)
        {
                $sql="select `sid` from `books` where `b_id`='$b_id'";
                $query=$this->db->query($sql);
                $result=$query->row_array();
                return $result['sid'];
        }

        function every_category_nums($cate_id)
        {
                if($cate_id!='')
                {
                        $sql="select `b_id` from `slave` where `type_id`='$cate_id'";
                        $query=$this->db->query($sql);
                        $every_category_nums=$query->num_rows();
                    return $every_category_nums;
                }
        }
        function every_category_list($num,$offset,$cate_id)
        {
                        $sql="select b.b_id,b.prod_id,b.name,b.author,b.publisher,b.price,b.pic,b.pub_date,b.star from books b  join slave s on b.b_id=s.b_id where s.type_id='$cate_id' order by b.b_id desc limit $offset,$num";
                        $query=$this->db->query($sql);
                        return $query->result_array();
        }
        function upload_setp_one()
        {
                $sub=$this->input->post('submit');
                if(isset($sub))
                {
                        $sid       =$this->input->post('sid');
                        $prod_id   =$this->input->post('prod_id');
                        $isbn      =$this->input->post('isbn');
                        $name      =$this->input->post('name');
                        $author    =$this->input->post('author');
                        $publisher =$this->input->post('publisher');
                        $price     =$this->input->post('price');
                        $yyyy      =$this->input->post('YYYY');
                        $mm        =$this->input->post('MM');
                        $dd        =$this->input->post('DD');
                        $ptime     =$yyyy.'-'.$mm.'-'.$dd;
                        $category[]=$this->input->post('tree1');
                        $tree2=$this->input->post('tree2');
                        if(isset($tree2)&&!empty($tree2))
                        {
                                foreach ($tree2 as $val)
                                {
                                        $category[]=$val;
                                }
                        }
                        $describe  =$this->input->post('describe');
                        $star      =$this->input->post('star') ? $this->input->post('star') : 0;
                        $sql="insert into `books` (`prod_id`,`isbn`,`name`,`author`,`publisher`,`price`,`describe`,`pub_date`,`star`, `sid`) values('$prod_id','$isbn','$name','$author','$publisher','$price','$describe','$ptime','$star','$sid')";
                        if($this->db->query($sql))
                        {
                                $sql3="select `b_id` from `books` where `isbn`='$isbn'";
                                $query=$this->db->query($sql3);
                                $row=$query->row_array();
                                $b_id=$row['b_id'];
                                //echo $b_id;
                                if(!empty($category))
                                {       foreach ($category as $cate_id)
                                        {
                                                $sql2="insert into `slave` (`b_id`,`type_id`) values('$b_id','$cate_id')";
                                                $this->db->query($sql2);
                                        }
                                }

                                return $b_id;
                        }
                        else
                        {
                                return false;
                        }

                }
        }
        function update_book_basic($b_id)
        {
                        $prod_id   =$this->input->post('prod_id');
                        $isbn      =$this->input->post('isbn');
                        $name      =$this->input->post('name');
                        $author    =$this->input->post('author');
                        $publisher =$this->input->post('publisher');
                        $price     =$this->input->post('price');
                        $ptime     =$this->input->post('ptime');
                        $category[]=$this->input->post('tree1');
                        $tree2=$this->input->post('tree2');
                        foreach ($tree2 as $val)
                        {
                                $category[]=$val;
                        }
                        $describe  =$this->input->post('describe');
                        $star      =$this->input->post('star');
                        $sql="update `books` set `prod_id`='$prod_id',`isbn`='$isbn',`name`='$name',`author`='$author',`publisher`='$publisher',`price`='$price',`describe`='$describe',`pub_date`='$ptime',`star`='$star' where `b_id`='$b_id'";
                        if($this->db->query($sql))
                        {
                                        $sql2="delete from `slave` where `b_id`='$b_id'";
                                        if($this->db->query($sql2))
                                        {
                                                if($category!='')
                                                {
                                                        foreach ($category as $cate_id)
                                                        {

                                                                $sql3="insert into `slave` (`b_id`,`type_id`) values('$b_id','$cate_id')";
                                                                $this->db->query($sql3);
                                                        }
                                                }
                                        }

                                return $b_id;
                        }
                        else
                        {
                                return false;
                        }
        }
        function del_book($b_id)
        {
                $sq="select * from `books` where `b_id`='$b_id'";
                $query=$this->db->query($sq);
                $msg=$query->row_array();
                $cover=$msg['pic'];
                $dirpath=$msg['dir_path'];
                $bookpath=$msg['b_path'];
                if($cover!='')
                {
                        unlink($cover);
                }
                if($dirpath!='')
                {
                        unlink($dirpath);
                }
                if($bookpath!='')
                {
                        unlink($bookpath);
                }
                $sql="delete from `books` where `b_id`='$b_id'";
                        if($this->db->query($sql))
                        {
                                $sql2="delete from `slave` where `b_id`='$b_id'";
                                if($this->db->query($sql2))
                                {
                                        return true;
                                }
                                else
                                {
                                        return false;
                                }
                        }
                        else
                        {
                                return false;
                        }
        }
        function get_books_num()
        {
                $sql="select `b_id` from `books` where `sid`<=0";
                $query=$this->db->query($sql);
                $b_num=$query->num_rows;
                return $b_num;
        }
        function show_books($num,$offset)
        {
                $sql="select `sid`, `b_id`,`prod_id`,`name`,`author`,`publisher`,`price`,`pic`,`pub_date`,`star` from `books` where `sid`<=0 order by `b_id` desc limit $offset,$num";
                $query = $this->db->query($sql);
                return $query->result_array();
        }

        function show_sub_books($sid) {
                $sql="select `sid`, `b_id`,`prod_id`,`name`,`author`,`publisher`,`price`,`pic`,`pub_date`,`star` from `books` where `sid`=$sid";
                $query = $this->db->query($sql);
                return $query->result_array();
        }

        function get_comments_nums($pid)
        {
                $sql="select `evaluate_id` from `evaluate` where `prod_id`='$pid'";
                $query=$this->db->query($sql);
                return $query->num_rows();
        }
        function get_ten_comments($pid)
        {
                $sql="select `evaluate_id`,`prod_id`,`level`,`comment`,`date` from `evaluate` where `prod_id`='$pid' order by `evaluate_id` DESC limit 10";
                $query=$this->db->query($sql);
                return $query->result_array();
        }
        function del_comment($pid,$com_id)
        {
                $sql="delete from `evaluate` where `prod_id`='$pid' and `evaluate_id`='$com_id'";
                if($this->db->query($sql))
                {
                        return true;
                }
        }
        function all_comments($limit,$offset,$pid)
        {
                $sql="select `evaluate_id`,`prod_id`,`level`,`comment`,`date` from `evaluate` where `prod_id`='$pid' order by `evaluate_id` DESC limit $offset,$limit";
                $query=$this->db->query($sql);
                $result=$query->result_array();
                return $result;
        }
        function search($limit,$offset,$key)
        {
                $sql="select `b_id`,`prod_id`,`name`,`author`,`publisher`,`price`,`pic`,`pub_date`,`star` from `books` where `name` like '%$key%' or `author` like '%$key%' order by `b_id` desc limit $offset,$limit";
                $query=$this->db->query($sql);
                $result=$query->result_array();
                return $result;
        }
        function search_num($key)
        {
                $sql="select `b_id` from `books` where `name` like '%$key%' or `author` like '%$key%'";
                $query=$this->db->query($sql);
                $num=$query->num_rows();
                return $num;
        }

}



















